package boogui.member;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JPasswordField;

public class S_MemberChangePwdDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JPasswordField pwdField;
	private BtnActionListener al;

	public S_MemberChangePwdDialog(MemberVO m) {
		setModal(true);
		al = new BtnActionListener();
		String id = m.getMemid();

		setTitle("비밀번호 변경");
		setSize(350, 200);
		setLocationRelativeTo(null);
		setResizable(false);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[] { 0, 0, 0 };
		gbl_contentPanel.rowHeights = new int[] { 0, 15, 0, 0 };
		gbl_contentPanel.columnWeights = new double[] { 0.0, 0.0 };
		gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, 0.0 };
		contentPanel.setLayout(gbl_contentPanel);
		{
			JLabel lblNewLabel = new JLabel("새로운 비밀번호를 입력하세요");
			GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
			gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel.gridx = 0;
			gbc_lblNewLabel.gridy = 0;
			contentPanel.add(lblNewLabel, gbc_lblNewLabel);
		}
		{
			pwdField = new JPasswordField();
			pwdField.setMargin(new Insets(3, 3, 3, 3));
			GridBagConstraints gbc_pwdField = new GridBagConstraints();
			gbc_pwdField.insets = new Insets(0, 0, 5, 5);
			gbc_pwdField.fill = GridBagConstraints.HORIZONTAL;
			gbc_pwdField.gridx = 0;
			gbc_pwdField.gridy = 2;
			contentPanel.add(pwdField, gbc_pwdField);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("변경하기");
				okButton.addActionListener(al);
				okButton.setActionCommand(id);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("취소");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
						dispose();
					}
				});
				buttonPane.add(cancelButton);
			}
		}

		setVisible(true);
	}

	private class BtnActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			IMemberFindDAO fd = new MemberFindDAO();
			if (fd.changePwd(e.getActionCommand(),new String(pwdField.getPassword()))) {
				JOptionPane.showMessageDialog(null, "비밀번호가 변경되었습니다.");
				setVisible(false);
				dispose();
			} else {
				JOptionPane.showMessageDialog(null, "비밀번호 변경에 실패하였습니다.");
			}
			
		}
	}
}
